//
//  ZRPictureCollectionViewCell.m
//  TestFile
//
//  Created by 黄涛 on 2018/7/26.
//  Copyright © 2018年 アポロジャパン. All rights reserved.
//

#import "ZRPictureCollectionViewCell.h"

@interface ZRPictureCollectionViewCell ()

@property (nonatomic , weak) UIImageView * imageView;
@property (nonatomic , weak) UIButton * stateBtn;

@end
@implementation ZRPictureCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self imageView];
        [self stateBtn];
    }
    return self;
}


#pragma mark - ----------------------- 其他方法 -----------------------
- (void)stateBtnClick:(UIButton *)sender{
    
    !_selectdBlock ? :self.selectdBlock(sender);
}
#pragma mark - ---------------------- set & get ----------------------
#pragma mark - 图片
- (UIImageView *)imageView{
    if (!_imageView) {
        
        UIImageView * imgView = [[UIImageView alloc] init];
        imgView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.contentView addSubview:imgView];
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.mas_equalTo(0);
        }];
        imgView.userInteractionEnabled = YES;
        
        _imageView = imgView;
    }
    return _imageView;
}

#pragma mark - 状态 按钮
- (UIButton *)stateBtn{
    if (!_stateBtn) {
        
        [self layoutIfNeeded];
        
        CGFloat w = (self.imageView.width - 8) / 4;
        
        UIButton * button = [[UIButton alloc] init];
        button.translatesAutoresizingMaskIntoConstraints = NO;
        [self.contentView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(5);
            make.right.mas_equalTo(-5);
            make.width.height.mas_equalTo(w);
        }];
        
        button.backgroundColor = ZRRandomColor;
        [button setImage:[UIImage imageNamed:@"201604111646345189"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(stateBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _stateBtn = button;
    }
    return _stateBtn;
}


#pragma mark - 获取模型
- (void)setModel:(ZRPictureSelectorModel *)model{
    _model = model;
    
    self.stateBtn.selected = model.isSelected;
    self.imageView.image = model.image;
}


@end
